package installinjava;

import java.awt.Color;
import java.awt.Font;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;

/**
 * Creates welcome panels.
 *
 * @author Gili Tzabari
 */
public class WelcomePanelBuilder
{
	private static final long serialVersionUID = 0L;
	// http://www.java-tips.org/java-se-tips/javax.swing/wrap-a-swing-jcomponent-in-a-background-image.html
	private JPanel banner;
	private JLabel background;
	private JLabel text;
	private JPanel buttons;

	/**
	 * Sets the banner at the top of the panel.
	 * 
	 * @param banner the banner at the top of the panel, null to disable
	 * @return the WelcomePanelBuilder
	 */
	public WelcomePanelBuilder background(JPanel banner)
	{
		this.banner = banner;
		return this;
	}

	/**
	 * Sets the background image.
	 *
	 * @param background the background image
	 * @return the WelcomePanelBuilder
	 */
	public WelcomePanelBuilder background(ImageIcon background)
	{
		if (background != null)
			this.background = new JLabel(background);
		else
			this.background = null;
		return this;
	}

	/**
	 * Sets the panel text.
	 *
	 * @param title the title text
	 * @param body the body text
	 * @return the WelcomePanelBuilder
	 */
	public WelcomePanelBuilder text(String title, String body)
	{
		if ((title == null || title.trim().isEmpty()) && (body == null || body.trim().isEmpty()))
		{
			this.text = null;
			return this;
		}
		StringBuilder buffer = new StringBuilder("<html>");
		if (title != null)
			buffer.append("<b>" + title + "</b><br/><br/>");
		if (body != null)
			buffer.append(body);
		buffer.append("</html>");
		this.text = new JLabel(buffer.toString());
		return this;
	}

	/**
	 * Sets the panel buttons.
	 *
	 * @param buttons the panel buttons, or null to disable
	 * @return the WelcomePanelBuilder
	 */
	public WelcomePanelBuilder buttons(JPanel buttons)
	{
		this.buttons = buttons;
		return this;
	}

	/**
	 * Returns the welcome panel.
	 *
	 * @return the welcome panel
	 */
	public JPanel build()
	{
		JPanel result = new JPanel();
		GroupLayout layout = new GroupLayout(result);
		result.setLayout(layout);

		JSeparator separator = new JSeparator();
		separator.setForeground(Color.BLACK);

		int textWidth;
		if (background != null)
			textWidth = background.getPreferredSize().width / 2;
		else
			textWidth = 0;

		ParallelGroup horizontalGroup = layout.createParallelGroup(Alignment.CENTER);
		if (text != null)
		{
			text.setHorizontalAlignment(SwingConstants.CENTER);
			text.setFont(new Font("Segoe UI", Font.PLAIN, 14));
			horizontalGroup = horizontalGroup.addGroup(layout.createSequentialGroup().
				addContainerGap(textWidth, Integer.MAX_VALUE).
				addComponent(text, GroupLayout.DEFAULT_SIZE, textWidth, GroupLayout.DEFAULT_SIZE).
				addContainerGap());
		}
		if (background != null)
		{
			background.setHorizontalAlignment(JLabel.CENTER);
			horizontalGroup = horizontalGroup.addComponent(background, GroupLayout.DEFAULT_SIZE,
				GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE);
		}
		if (buttons != null)
		{
			horizontalGroup = horizontalGroup.addComponent(separator, GroupLayout.DEFAULT_SIZE,
				GroupLayout.DEFAULT_SIZE, Integer.MAX_VALUE).
				addComponent(buttons, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE);
		}
		layout.setHorizontalGroup(horizontalGroup);

		ParallelGroup verticalImage = layout.createParallelGroup();
		if (text != null)
		{
			verticalImage = verticalImage.addGroup(layout.createSequentialGroup().
				addContainerGap().
				addComponent(text, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE).
				addContainerGap());
		}
		if (background != null)
		{
			verticalImage.addComponent(background, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE,
				GroupLayout.DEFAULT_SIZE);
		}
		SequentialGroup verticalGroup = layout.createSequentialGroup().addGroup(verticalImage);
		if (buttons != null)
		{
			verticalGroup = verticalGroup.addComponent(separator).addPreferredGap(ComponentPlacement.UNRELATED).
				addComponent(buttons).addContainerGap();
		}
		layout.setVerticalGroup(verticalGroup);
		return result;
	}
}
